package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Partner;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

/**
 * 合作伙伴(销售单位等)
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/com")
public class PartnerController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/partnerList")
	public ModelAndView getMyPartnerList() {

		PageInfo<Partner> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Partner> list = new ArrayList<Partner>();
			Partner partner = new Partner();
			partner.setId(1);
			partner.setCompanyName("销售单位一");
			partner.setPinyin("xsdwy");
			partner.setLinkMam("联系人一");
			partner.setLinkTel("88885222");
			partner.setAddress("单位地址一");
			list.add(partner);

		} else {

			pageInfo = companyService.getPartnerListByComId(AppConstants.comId,
					1, AppConstants.DEFAULT_SIZE);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/partner/getUsableList")
	public ModelAndView getUsablePartnerList() {

		List<Partner> list = null;
		int total = 0;
		if (AppConstants.DEBUG) {
			list = new ArrayList<Partner>();
			for (int i = 0; i < 10; i++) {

				Partner partner = new Partner();
				partner.setId(i);
				partner.setCompanyName("秋涛店" + i);
				list.add(partner);
			}
			total = list.size();

		} else {

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", list);
		model.addAttribute("totalProperty", total);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping(value="/createPartner",method=RequestMethod.POST)
	public ModelAndView save(@RequestBody Partner partner) {
		
		if (!AppConstants.DEBUG) {
			
		
		}else{
			partner.setCompanyName(partner.getCompanyName().trim());
			partner.setAddress(partner.getAddress().trim());
			partner.setLinkMam(partner.getLinkMam().trim());
			partner.setLinkTel(partner.getLinkTel().trim());
			companyService.createPartner(partner);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updatePartner")
	public ModelAndView update(@RequestBody Partner partner) {

		if (!AppConstants.DEBUG) {


		} else {
			
			partner.setCompanyName(partner.getCompanyName().trim());
			partner.setAddress(partner.getAddress().trim());
			partner.setLinkMam(partner.getLinkMam().trim());
			partner.setLinkTel(partner.getLinkTel().trim());
			
			companyService.updatePartner(partner);

		}
		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

}
